<?php

class Laporan_peternakan extends CI_Controller {
	
	function Laporan_peternakan()
	{
		parent::__construct();
		$this->load->model('Home_model', '', TRUE);
		$this->load->model('auth_model', '', TRUE);
		$this->load->model('lokasi_model', '', TRUE);
		$this->load->model('satuan_model', '', TRUE);
		$this->load->model('peternakan_model', '', TRUE);
		$this->load->model('laporan/lap_peternakan', '', TRUE);
	}
	
	function index()
	{
		if ($this->session->userdata('login') == FALSE)
		{
			redirect('auth');
		}
		
		
		
		// Load default view
		$id_role = $this->session->userdata('id_role'); 
		$data['title'] = "Laporan Peternakan";
		$data['main_view'] = 'laporan/lap_peternakan/lap_peternakan_view';
		$data['hak_akses0'] 				= $this->Home_model->get_hak_akses($id_role,0);
		$data['hak_akses1'] 				= $this->Home_model->get_hak_akses($id_role,1);
		$data['hak_akses2'] 				= $this->Home_model->get_hak_akses($id_role,2);
		
		$data['dist_masuk'] 				= $this->lap_peternakan->get_distribusi_masuk();
		$data['dist_keluar']				= $this->lap_peternakan->get_distribusi_keluar();
		
		$data['pertambahan_pengurangan'] 	= $this->lap_peternakan->get_pertambahan_pengurangan();
		$data['max_pp'] 					= $this->lap_peternakan->get_max_pp();
		$data['hasil_hitung']				= $this->lap_peternakan->hasil_hitung($data['dist_masuk'],$data['dist_keluar']);
		$data['hasil_fix']					= $this->lap_peternakan->hasil_fix($data['hasil_hitung']);
		$data['mapping_hasil']				= $this->lap_peternakan->mapping_hasil($data['hasil_fix']);
		$data['max_dist'] 					= $this->lap_peternakan->get_max_dist();
		$data['kota']						= $this->lap_peternakan->get_kota();
		//var_dump($data['dist_keluar']);
		//print_r($data["pp_max"]->result_array());
		//echo $data["max_pp"]["J"];
		$this->load->view('template', $data);
	}
	
	function add_rph(){
	
		$tanggal = $this->input->post('tanggal_berdiri');
		$tanggal_arr = explode('/', $tanggal);
		$tanggal = $tanggal_arr[2]."-".$tanggal_arr[0]."-".$tanggal_arr[1];
	
		$arr = array(
			'NAMA' => $this->input->post('nama'),
			'ALAMAT' => $this->input->post('alamat'),
			'ID_KOTA' => $this->input->post('id_kota'),
			'NO_TELP' => $this->input->post('no_telp'),
			'EMAIL' => $this->input->post('email'),
			'KODE_POS' => $this->input->post('kode_pos'),
			'TANGGAL_BERDIRI' => $tanggal,
			'LAT' => $this->input->post('lat'),
			'LON' => $this->input->post('lng'),
			'INFO' => $this->input->post('info')
		);
		
		$id_titik_pasok = $this->rph_model->insert_titik_pasok($arr);
		
		$arr2 = array(
			'ID_TITIK_PASOK' => $id_titik_pasok,
			'ID_SATUAN' => $this->input->post('id_satuan'),
			'SAPI_MASUK_NORMAL' => $this->input->post('sapi_masuk_normal'),
			'PRODUKSI_NORMAL' => $this->input->post('produksi_normal'),
			'SAT_ID_SATUAN' => $this->input->post('sat_id_satuan'),
			'LISENSI' => $this->input->post('lisensi')
		);
		
		$this->rph_model->insert_rph($arr2);
		
	}
	
	function update_rph(){
		$id = $this->input->post('id_rph');
		$submit = $this->input->post('submit');
		if($submit == "false"){
			$data['rph'] = $this->rph_model->get_rph($id);
			$this->load->view('rph/rph_edit', $data);
		} else {
			
		}
		
	}
	
	function delete_rph(){
		$id_rph = $this->input->post('id_rph');
		$id_pasok = $this->input->post('id_titik_pasok');
		$this->rph_model->delete_rph($id_rph, $id_pasok);
	}
	
	function update_role(){
		$id = $this->input->post('id_role');
		$data['role_res'] = $this->role_model->get_role($id);
		$this->load->view('role/role_edit', $data);
		
	}
	
	function submit_role(){
		$id = $this->input->post('id_role');
		$arr = array(
			'NAMA_ROLE' => $this->input->post('nama_role')
		);
		
		$this->role_model->update_role($id, $arr);
		
		redirect('role');
	}
	
}